/* =========================================================================
 *
 *   The traffic processor.
 *
 *   Development by Sedelnikov Alexander (Bigking).
 *
 * =========================================================================
 */

#ifndef __OUT_H__
#define __OUT_H__


#include "conf.h"
#include "trf_parser.h"
#include "transact.h"
#ifdef POSTGRES
#include <libpq-fe.h>
#endif


//------------------------------------------------------------------------//
class cout_info
{


protected:

    char        label[ 512 ];


public:

    cout_info() {    }
    virtual ~cout_info() {    }

    void   set_label( const char *lbl )
    {
        strncpy( label, lbl, 511 );
        label[ 511 ] = 0;
    }

    const char* get_label( ) const
    {
        return label;
    }

    static void format_traffic( const trf_gth    &trf,
                                const time_stamp &ts,
                                const char *format,
                                char *str_ptr,
                                int   max_len );

    virtual void start() = 0;
    virtual int  exec( const char *ptr ) = 0;
    virtual void send_line( const char *str, int length ) = 0;
    virtual void end( ctransaction *trans )  = 0;
    virtual void finish() = 0;


//------------------------------------------------------------------------//
EXCEPTION( e_start );
EXCEPTION( e_send_line );
EXCEPTION( e_end );


};


//------------------------------------------------------------------------//
class cout_file : public cout_info
{


    char        unical_name[ 128 ];

    FILE       *fp;


public:

    cout_file();
    virtual ~cout_file();

    void start();
    int  exec( const char *ptr );
    void send_line( const char *str, int length );
    void end( ctransaction *trans );
    void finish();


};


//------------------------------------------------------------------------//
class cout_postgres : public cout_info
{


#ifdef POSTGRES
    PGconn   *pgsql;

#define PGSQL_CHECK(res,msg) \
    if( res == NULL ) \
    { \
        THROW( e_start, msg ); \
    } \
    if( PQresultStatus( res ) == PGRES_NONFATAL_ERROR || \
                       PQresultStatus( res ) == PGRES_FATAL_ERROR ) \
    { \
        THROW( e_start, msg " [%s]", PQresultErrorMessage( res ) ); \
    }

#endif


public:

    cout_postgres();
    virtual ~cout_postgres();

    void start();
    int  exec( const char *ptr );
    void send_line( const char *str, int length );
    void end( ctransaction *trans );
    void finish();


};


#endif
